'use strict';

// 断言
const assert = require('assert');
// 小程序入口文件
const funs = require('../index.js');
// 本地mongodb
const mf_mongodb = require('../localExploit/connect_local_mongodb.js');
// top接口初始化
const ApiClient = require('../localExploit/lib/taobao.js').ApiClient; // 淘宝API
// 本地mysqldb
const mf_mysqldb = require('../localExploit/connect_local_mysqldb.js');

// Top接口使用appkey
const topApi = new ApiClient({
  appkey: '28370694',
  appsecret: '12342f554ecd9851da05743558f0dee8',
  url: 'http://gw.api.taobao.com/router/rest',
});

// 参数
const ctx = {
  'platform': 'taobao',
  'env': 'node',
  'mixNick': 'z01IE98JVR+EUTRXq0dG0i0pTix+Oonf5VD0dnSUq8vAJo=',
  'data': {
    'id': '1',
  },
  'cloud': {}
};

describe('#进行单元测试', () => {
  describe('main函数', () => {
    it('正常参数', async () => {
      let mfmongodb, mfmysqldb, data;
      try {
        // 增加mongodb
        mfmongodb = await mf_mongodb();
        ctx.cloud.db = mfmongodb.mongodb;
        // 增加mysqldb
        mfmysqldb = await mf_mysqldb();
        ctx.cloud.dataspace = mfmysqldb;
        // 增加top接口
        ctx.cloud.topApi = topApi;
        ctx.topSession = '50000001524bkjkdWvBRTgBFCpfNWyEKlyna11f01f46Gx1HhZHvQbRvaZC0GWRtDLc';
        // 执行函数
        data = await funs.main(ctx);
        console.log(data);
      } catch (err) {
        console.log("出现异常:",JSON.stringify(err));
      } finally {
        mfmongodb.db.close();
        mfmysqldb.end();
      }
      assert(data)
      assert(data === 1)

    });
  });
})